package com.ruoyi.projects.service.impl;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.projects.Vos.WxUserInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.projects.mapper.SlCustomMapper;
import com.ruoyi.projects.domain.SlCustom;
import com.ruoyi.projects.service.ISlCustomService;

/**
 * 森理会员Service业务层处理
 * 
 * @author yangz
 * @date 2024-10-14
 */
@Service
public class SlCustomServiceImpl implements ISlCustomService 
{
    @Autowired
    private SlCustomMapper slCustomMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public WxUserInfoDTO saveWxUserInfo(WxUserInfoDTO dto,WxUserInfoDTO userInfo) {
        // 查询是否已存在该用户
        SlCustom customer = slCustomMapper.selectSlCustomByOpenId(userInfo.getOpenid());
        if (customer == null) {
            // 新用户，创建记录
            customer = new SlCustom();
            customer.setOpenId(userInfo.getOpenid());
            // 更新用户信息
            customer.setNickname(userInfo.getNickName());
            customer.setAvatarurl(userInfo.getAvatarUrl());
            customer.setGender(userInfo.getGender()!=null?userInfo.getGender().toString():"1");
            customer.setCity(userInfo.getCity());
            customer.setUpdateTime(DateUtils.getNowDate());
            customer.setCreateTime(DateUtils.getNowDate());
            slCustomMapper.insertSlCustom(customer);
        }else {
            customer.setNickname(userInfo.getNickName());
            customer.setAvatarurl(userInfo.getAvatarUrl());
            customer.setGender(userInfo.getGender()!=null?userInfo.getGender().toString():"1");
            customer.setCity(userInfo.getCity());
            customer.setUpdateTime(DateUtils.getNowDate());
            slCustomMapper.updateSlCustom(customer);
        }
        userInfo.setId(customer.getId());
        userInfo.setGender(Integer.parseInt(customer.getGender()));
        userInfo.setIntegral(customer.getIntegral());
        // 保存token到Redis，设置过期时间
        userInfo.setToken(dto.getToken());
        userInfo.setIntegral(customer.getIntegral());
        redisCache.setCacheObject(BuConstants.MINITOKEN + dto.getToken(), userInfo, 7, TimeUnit.DAYS);
        return userInfo;
    }

    /**
     * 查询森理会员
     * 
     * @param id 森理会员主键
     * @return 森理会员
     */
    @Override
    public SlCustom selectSlCustomById(Long id)
    {
        return slCustomMapper.selectSlCustomById(id);
    }

    /**
     * 根据openid查询微信会员
     * @param openId
     * @return
     */
    @Override
    public SlCustom selectSlCustomByOpenId(String openId)
    {
        return slCustomMapper.selectSlCustomByOpenId(openId);
    }

    /**
     * 查询森理会员列表
     * 
     * @param slCustom 森理会员
     * @return 森理会员
     */
    @Override
    public List<SlCustom> selectSlCustomList(SlCustom slCustom)
    {
        return slCustomMapper.selectSlCustomList(slCustom);
    }

    /**
     * 新增森理会员
     * 
     * @param slCustom 森理会员
     * @return 结果
     */
    @Override
    public int insertSlCustom(SlCustom slCustom)
    {
        slCustom.setCreateTime(DateUtils.getNowDate());
        return slCustomMapper.insertSlCustom(slCustom);
    }

    /**
     * 修改森理会员
     * 
     * @param slCustom 森理会员
     * @return 结果
     */
    @Override
    public int updateSlCustom(SlCustom slCustom)
    {
        slCustom.setUpdateTime(DateUtils.getNowDate());
        return slCustomMapper.updateSlCustom(slCustom);
    }

    /**
     * 批量删除森理会员
     * 
     * @param ids 需要删除的森理会员主键
     * @return 结果
     */
    @Override
    public int deleteSlCustomByIds(Long[] ids)
    {
        return slCustomMapper.deleteSlCustomByIds(ids);
    }

    /**
     * 删除森理会员信息
     * 
     * @param id 森理会员主键
     * @return 结果
     */
    @Override
    public int deleteSlCustomById(Long id)
    {
        return slCustomMapper.deleteSlCustomById(id);
    }
}
